package www.dianbaozu.com.ui.my.personalHomepage

import android.annotation.SuppressLint
import android.view.View
import androidx.fragment.app.viewModels
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.FlowPreview
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentPersonalDetailsBinding
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.extension.finishMyself
import www.wen.com.common.lib.extension.glide.setImageUrl
import www.wen.com.common.lib.extension.imgUrl
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter

@AndroidEntryPoint
class PersonalHomepageFragment : BaseLoadingFragment<FragmentPersonalDetailsBinding>() {
    private val userId by lazy { requireActivity().intent.getStringExtra("userId") ?: "" }

    private val mBaseApiViewModel: PersonalHomepageViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = mBaseApiViewModel

    override fun isEnableLoadingHelper(): Boolean = true

    override fun actionLeft() {
        finishMyself()
    }

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter =
        TitleBarUniversalAdapter(requireActivity(), "个人主页", action = ::titleAction)

    override fun lazyInitView() {
        dataBinding.apply {
            homepageViewModel = mBaseApiViewModel
            tvSave.visibility = View.GONE
            imageView7.visibility = View.INVISIBLE
            tvGender.setCompoundDrawables(null, null, null, null)
            tvBirthday.setCompoundDrawables(null, null, null, null)
            tvArea.setCompoundDrawables(null, null, null, null)
            tvMail.setCompoundDrawables(null, null, null, null)
            requestUserInfoById()
        }
    }



    override fun getLayoutId(): Int = R.layout.fragment_personal_details

    @SuppressLint("SetTextI18n")
    @FlowPreview
    fun requestUserInfoById() {
        mBaseApiViewModel.requestUserInfoById(userId) {
            dataBinding.apply {
                setImageUrl(ivProfileAvatar, it.data.avatar.imgUrl())
                tvInputNickname.text = it.data.nickName
                tvInputSignature.text = it.data.intro
                tvGender.text = if (it.data.gender == 1) "男" else "女"
                tvBirthday.text = it.data.birthday
                tvArea.text = "${it.data.province}${it.data.city}"
                tvMail.text = it.data.email
            }
        }
    }

}